<template>
  <el-aside :width="isCollapse ? '64px' : '200px'">
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        background-color="#001529"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        :collapse="isCollapse"
        :collapse-transition="false"
        router
        unique-opened
      >
        <sidebar-item
          v-for="route in processedRoutes"
          :key="route.path"
          :item="route"
          :base-path="route.path"
        />
      </el-menu>
    </el-scrollbar>
  </el-aside>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import SidebarItem from './SidebarItem.vue'
import { usePermissionStore } from '@/stores/permission'

const permissionStore = usePermissionStore()
const route = useRoute()

// 深度过滤路由配置
// 修改过滤条件（第22-25行）
const processedRoutes = computed(() => {
  const filterRoutes = (routes) => {
    return routes.filter(route => {
      const hasValidComponent = !!route.component
      const hasVisibleChildren = route.children?.some(child => !child.meta?.hidden)
      return !route.meta?.hidden && (hasValidComponent || hasVisibleChildren)
    }).map(route => ({
      ...route,
      path: route.path?.replace(/^\//, '') || '', // 新增路径格式化
      children: route.children ? filterRoutes(route.children) : []
    }))
  }
  return filterRoutes(permissionStore.routes)
})

// 当前激活菜单计算
const activeMenu = computed(() => {
  return route.meta.activeMenu || route.path
})

// 接收折叠状态
const props = defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
})
</script>

<style lang="scss" scoped>
.el-aside {
  transition: width 0.3s ease;
  
  .el-menu {
    border-right: none;
    
    &:not(.el-menu--collapse) {
      width: 200px;
    }
  }
}
</style>